﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AlexandriaPressUserBusinessLayer;
using AlexandriaPressUserObject;

namespace AlexandriaPressWebsite.MasterPages.UserPages
{
    public partial class TitleDetailsPage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["newOrders"] != null)
            {
                Session.Remove("newOrders");
            }

            int identifier = 0;
            decimal price = 0;
            int stock = 0;
            string title = "";
            try
            {
                DetailsGridView.DataSource = UserBusinsessLayer.getDetails(Convert.ToInt32(Request.QueryString["id"]), ref identifier, ref price, ref stock, ref title);
                DetailsGridView.DataBind();
                CommentsGridView.EmptyDataText = "No comments are available at this time.";
                CommentsGridView.DataSource = UserBusinsessLayer.getComments(Convert.ToInt32(Request.QueryString["id"]));
                CommentsGridView.DataBind();
                Session.Add("productID", identifier);
                Session.Add("pricePerUnit", price);
                Session.Add("stock", stock);
                Session.Add("title", title);
            }
            catch
            {
                Response.Redirect("~/MasterPages/MainPage.aspx");
            }
            if (Session["userID"] == null)
            {
                ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("formDoc");
                content.Visible = false;                
            }
        }

        protected void buyButton_Click(object sender, EventArgs e)
        {
            if (Convert.ToInt32(numUnitsBox.Text) != 0)
            {
                if (numUnitsBox.Text.Trim() != "")
                {
                    if (Convert.ToInt32(Session["stock"]) < Convert.ToInt32(numUnitsBox.Text.Trim()))
                    {
                        notificationLabel.Text = "There is not enough of the item in stock";
                    }
                    else
                    {
                        List<OrderItems> orders = null;
                        int numItems = 0;
                        OrderItems item = new OrderItems();
                        item.BookID = Convert.ToInt32(Session["productID"]);
                        item.Title = Convert.ToString(Session["title"]);
                        item.Price = Convert.ToInt32(Session["pricePerUnit"]);
                        item.Quantity = Convert.ToInt32(numUnitsBox.Text.Trim());

                        if (Session["orders"] == null)
                        {
                            orders = new List<OrderItems>();
                            orders.Add(item);
                        }
                        else
                        {
                            bool unique = true;
                            orders = (List<OrderItems>)Session["orders"];
                            foreach (OrderItems book in orders)
                            {
                                //if (item.BookID == book.BookID)
                                //{
                                //    book.Quantity += item.Quantity;
                                //    unique = false;
                                //    break;
                                //}
                                book.Quantity = UserBusinsessLayer.findUnique(item, book, ref unique);
                                if (!unique) 
                                {
                                    break;
                                }
                            }
                            if (unique)
                            {
                                orders.Add(item);
                            }
                        }

                        Session.Remove("productID");
                        Session.Remove("pricePerUnit");
                        Session.Remove("stock");
                        Session.Remove("title");
                        Session.Add("newOrders", orders);

                        foreach (OrderItems book in orders)
                        {
                            numItems += book.Quantity;
                        }

                        Session.Add("numItems", numItems);

                        Response.Redirect("ShoppingCart.aspx");
                    }
                }
                else
                {
                    notificationLabel.Text = "This field is required.";
                }
            }
            else 
            {
                notificationLabel.Text = "Please specify the quantity";
            }
        }

        protected void CommentsGridView_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            CommentsGridView.PageIndex = e.NewPageIndex;
            CommentsGridView.EmptyDataText = "No comments are available at this time.";
            CommentsGridView.DataSource = UserBusinsessLayer.getComments(Convert.ToInt32(Request.QueryString["id"]));
            CommentsGridView.DataBind();
        }

        protected void commentButton_Click(object sender, EventArgs e)
        {
            ratingLabel.Visible = false;
            Rating.Visible = false;
            commentsLabel.Visible = false;
            commentArea.Visible = false;
            commentButton.Visible = false;
            commentSeparatorLabel.Visible = false;
            string comment = commentArea.Value.Trim();
            int i = UserBusinsessLayer.postComment(Convert.ToInt32(Session["userID"]), Convert.ToInt32(Session["productID"]), Convert.ToInt32(Rating.SelectedItem.Value), comment);
            if (i > 0)
            {

                MessageLabel.Text = "Your comment has been saved. Thank you!<br/><br/>";
            }
            else
            {
                MessageLabel.Text = "An error with passing your input occurred. Please try again later.<br/><br/>";
            }
            MessageLabel.Visible = true;
        }

    }
}
